#pragma once
#include <UIlib.h>
#include <windows.h>
#include <functional>

using namespace DuiLib;

class PreviewImageUI : public CControlUI
{
public:
    enum SelectState
    {
        SELECT_NONE,
        Select_DRAG,
        SELECTING,
        SELECT_FIRST_CLICK,
        SELECT_COMPLETE,
        SELECT_GET_COLOR,
    };

    typedef std::function<void(RECT selectedRC)> OnSelectCompleteCallback;
    typedef std::function<void(RECT rc)> OnSelectingCallback;
    typedef std::function<void(COLORREF color)> OnGetColorCallback;

    PreviewImageUI();
    virtual ~PreviewImageUI();

    // 设置HBITMAP
    void SetBitmap(HBITMAP hBitmap);

    // 清除位图
    void ClearBitmap();

    // 获取当前矩形区域
    RECT GetSelectedRect() const { return m_selectedRect; }

    void StartSelect();

    void StartGetColor()
    {
        m_selectState = SELECT_GET_COLOR;
    }

    void SetSelectCompleteCallback(OnSelectCompleteCallback callback) { m_onSelectComplete = callback; }

    void SetSelectingCallback(OnSelectingCallback callback) { m_onSelecting = callback; }

    void SetGetColorCallback(OnGetColorCallback callback) { m_onGetColor = callback; }

    HBITMAP GetBitmap() const { return m_hBitmap; }

protected:
    // 消息处理
    virtual void DoEvent(TEventUI &event) override;

    // 绘制函数
    virtual bool DoPaint(HDC hDC, const RECT &rcPaint, CControlUI *pStopControl) override;

    UINT GetControlFlags() const override;

private:
    // 获取客户端区域
    RECT GetClientRect() const;

    void NotifySelectComplete();

    RECT CalcSelectRect();

private:
    HBITMAP m_hBitmap;

    int m_bmpWidth = 0;
    int m_bmpHeight = 0;

    float m_fscale = 1.0f;

    SelectState m_selectState = SELECT_NONE;

    POINT m_startPoint;
    POINT m_endPoint;
    RECT m_selectedRect;

    bool m_bCanDrag = true;

    POINT m_bmpDrawPoint;

    OnSelectCompleteCallback m_onSelectComplete;

    OnSelectingCallback m_onSelecting;

    OnGetColorCallback m_onGetColor;
};
